package com.zsx.shopping.seckill.controller;

import com.zsx.shopping.common.api.R;
import com.zsx.shopping.seckill.service.SeckillService;
import com.zsx.shopping.seckill.vo.app.CurSeckillSkuVO;
import com.zsx.shopping.seckill.vo.app.SeckillGoodsVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Resource
    private SeckillService seckillService;

    @PostMapping("/seckill")
    public R<String> seckill(@RequestParam Long sessionId, @RequestParam Long skuId,
                             @RequestParam Integer num, @RequestParam String code) {
        return R.success(seckillService.kill(sessionId, skuId, num, code));
    }

    @GetMapping("/curSeckillSkus")
    public R<List<CurSeckillSkuVO>> getCurSeckillSkus() {
        return R.success(seckillService.getCurSeckillSkus());
    }

    @GetMapping("/seckillGoods")
    public R<SeckillGoodsVO> getSeckillGoods(@RequestParam Long skuId) {
        return R.success(seckillService.getSeckillGoods(skuId));
    }

    @PutMapping("/order/seckillStatus")
    public R<Void> changeSeckillStatus(@RequestParam Long userId, @RequestParam Long sessionId,
                                       @RequestParam Long skuId, @RequestParam Byte status) {
        seckillService.changeSeckillStatus(userId, sessionId, skuId, status);
        return R.success();
    }

    @GetMapping("/seckillStatus")
    public R<Byte> getSeckillStatus(@RequestParam Long sessionId, @RequestParam Long skuId) {
        return R.success(seckillService.askSeckillStatus(sessionId, skuId));
    }

    @PutMapping("/restoreStock")
    public R<Void> restoreStock(@RequestParam String randomCode, @RequestParam Integer num) {
        seckillService.restoreStock(randomCode, num);
        return R.success();
    }
}
